#include "delay.h"
#include "ds1302.h"
#include "lcd1602.h"

void main() 
{
	unsigned char buf[3] = {' ',' ',':'};
	unsigned char buf2[3] = {' ',' ','-'};
	unsigned char tmp;

	ds1302_write_byte(0x8E, 0x00); 
	ds1302_write_byte(0x80, 0x42); //set second
	ds1302_write_byte(0x82, 0x10); //min
	ds1302_write_byte(0x84, 0x8A); //hour
	ds1302_write_byte(0x86, 0x02); //date
	ds1302_write_byte(0x88, 0x12); //month
	ds1302_write_byte(0x8A, 0x01); //day
	ds1302_write_byte(0x8C, 0x24); //year
	LCD_Init();
	while(1) 
	{
		LCD_Display("20", 2, 0, 2);
		
		tmp = ds1302_read_byte(0x8D); //year
		buf2[0] = ((tmp & 0xF0) >> 4) + 0x30 ;
		buf2[1] = (tmp & 0x0F) + 0x30 ;
		LCD_Display(buf2, 2, 2, 3);
		
		tmp = ds1302_read_byte(0x89); //month
		buf2[0] = ((tmp & 0x10) >> 4) + 0x30 ;
		buf2[1] = (tmp & 0x0F) + 0x30 ;
		LCD_Display(buf2, 2, 5, 3);
		
		tmp = ds1302_read_byte(0x87); //date
		buf2[0] = ((tmp & 0x30) >> 4) + 0x30 ;
		buf2[1] = (tmp & 0x0F) + 0x30 ;
		LCD_Display(buf2, 2, 8, 2);
		
		tmp = ds1302_read_byte(0x85); //hour
		buf[0] = ((tmp & 0x10) >> 4) + 0x30 ;
		buf[1] = (tmp & 0x0F) + 0x30 ;
		LCD_Display(buf, 1, 0, 3);
		
		tmp = ds1302_read_byte(0x83); //min
		buf[0] = ((tmp & 0x70) >> 4) + 0x30 ;
		buf[1] = (tmp & 0x0F) + 0x30 ;
		LCD_Display(buf, 1, 3, 3);
		
		tmp = ds1302_read_byte(0x81);//sec
		buf[0] = ((tmp & 0x70) >> 4) + 0x30 ;
		buf[1] = (tmp & 0x0F) + 0x30 ;
		LCD_Display(buf, 1, 6, 2);
		
		
		
		delay(99);
	}
}